const query = require('./fun')
const sqlCount = 'SQL_CALC_FOUND_ROWS';
const { SWIPER, USER, INFO, PRAISE, INFOCOMM, ADMIN, RTICLEA, COLLECTION, KNOWCOMM, SENSITIVES } = require('./database')
let { sqlUpdateStr, sqlAddStr, sqlSelectStr, sqlWhereStr, sqlAddStr_null } = require('../util.js');
/**
 * 获取所有轮播图
 */
function getSwiperList() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${SWIPER} ORDER BY swiperIndex ASC`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 注册用户
 */
function register(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${USER}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 判断是否存在用户
 */
function isUser(data) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${USER} where userPhone=?`, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取帖子
 */
function getInfoList(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select  ${sqlCount} * from ${INFO} ${condition} order by infoId desc  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 帖子详情
 */
function infoDetail(infoId) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${INFO}.infoId`]: infoId,
            [`${INFO}.userId`]: `${USER}.userId`
        });
        query(`select ${INFO}.*,${USER}.userName,${USER}.userUrl from ${INFO},${USER} ${condition}`)
            .then((res) => {
                resolve(res[0])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 判断是否点赞
 */
function isaddPraise(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select * from ${PRAISE} ${sql}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取点赞数目
 */
function fabulousNum(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select count(*) as counts from ${PRAISE} ${sql}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取评论数目
 */
function commNum(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select count(*) as counts from ${INFOCOMM} ${sql}`)
            .then((res) => {
                resolve(res[0]['counts'])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 取消点赞
 */
function delPraise(praiseId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${PRAISE} WHERE praiseId=?`, { praiseId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 点赞
 */
function addPraise(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${PRAISE}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 用户评论帖子
 */
function addInfoComm(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${INFOCOMM}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 用户用户评论
 */
function getInfoComm(num, limit, data) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${INFOCOMM}.userId`]: `${USER}.userId`,
            [`${INFOCOMM}.infoId`]: data.infoId
        });
        query(`select  ${sqlCount} ${INFOCOMM}.*,${USER}.userName,${USER}.userUrl from ${INFOCOMM},${USER} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取文章信息
 */
function getKnowList(num, limit) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${RTICLEA}.adminId`]: `${ADMIN}.id`
        });
        query(`select  ${sqlCount} ${RTICLEA}.*,${ADMIN}.name,${ADMIN}.url from ${RTICLEA},${ADMIN} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 判断是否收藏
 */
function isCollection(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select * from ${COLLECTION} ${sql}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 判断是否收藏
 */
function isaaddFollow(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select * from ${COLLECTION} ${sql}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 取消收藏
 */
function delFollow(collectionId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${COLLECTION} WHERE collectionId=?`, { collectionId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 收藏
 */
function addFollow(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${COLLECTION}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 帖子详情
 */
function knowDetail(rticleaId) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${RTICLEA}.rticleaId`]: rticleaId,
            [`${RTICLEA}.adminId`]: `${ADMIN}.id`
        });
        query(`select ${RTICLEA}.*,${ADMIN}.name,${ADMIN}.url from ${RTICLEA},${ADMIN} ${condition}`)
            .then((res) => {
                resolve(res[0])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 用户评论帖子
 */
function addKnowComm(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${KNOWCOMM}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 用户用户评论
 */
function getKnowComm(num, limit, data) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${KNOWCOMM}.userId`]: `${USER}.userId`,
            [`${KNOWCOMM}.rticleaId`]: data.rticleaId
        });
        query(`select  ${sqlCount} ${KNOWCOMM}.*,${USER}.userName,${USER}.userUrl from ${KNOWCOMM},${USER} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id获取用户信息
 */
function GetUsersById(id) {
    return new Promise((resolve, reject) => {
        query(`select * from ${USER} where userId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加帖子
 */
function addInfo(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr_null(data);
        query(`INSERT INTO ${INFO}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取点赞人
 */
function getLikeList(num, limit, data) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${PRAISE}.userId`]: `${USER}.userId`,
            [`${PRAISE}.infoId`]: `${INFO}.infoId`
            , [`${INFO}.userId`]: data.userId
        });
        query(`select  ${INFO}.*,${USER}.userName,${USER}.userUrl from ${INFO},${USER},${PRAISE} ${condition}  limit ?,${limit}`, { num })
            .then(async (res) => {
                await Promise.all((res.map(item => {
                    return new Promise(async (res, rej) => {
                        let data = await GetUsersById(item.userId)
                        item.buser = data[0]
                        res()
                    })
                })))
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取评论
 */
function getCommList(num, limit, data) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${INFOCOMM}.userId`]: `${USER}.userId`,
            [`${INFOCOMM}.infoId`]: `${INFO}.infoId`
            , [`${INFO}.userId`]: data.userId
        });
        query(`select  ${INFO}.*,${USER}.userName,${USER}.userUrl,${INFOCOMM}.* from ${INFO},${USER},${INFOCOMM} ${condition}  limit ?,${limit}`, { num })
            .then(async (res) => {
                await Promise.all((res.map(item => {
                    return new Promise(async (res, rej) => {
                        let data = await GetUsersById(item.userId)
                        item.buser = data[0]
                        res()
                    })
                })))
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取所有敏感词
 */
function getSensitiveAll() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${SENSITIVES}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取我的收藏
 */
function getCollectionList(num, limit, data) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${COLLECTION}.rticleaId`]: `${RTICLEA}.rticleaId`
            , [`${COLLECTION}.userId`]: data.userId
        });
        query(`select  ${RTICLEA}.* from ${COLLECTION},${RTICLEA} ${condition}  limit ?,${limit}`, { num })
            .then(async (res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
exports.getCollectionList = getCollectionList;
exports.getSensitiveAll = getSensitiveAll;
exports.getCommList = getCommList;
exports.getLikeList = getLikeList;
exports.addInfo = addInfo;
exports.getKnowComm = getKnowComm;
exports.addKnowComm = addKnowComm;
exports.getSwiperList = getSwiperList;
exports.register = register;
exports.isUser = isUser;
exports.getInfoList = getInfoList;
exports.infoDetail = infoDetail;
exports.isaddPraise = isaddPraise;
exports.fabulousNum = fabulousNum;
exports.delPraise = delPraise;
exports.addPraise = addPraise;
exports.addInfoComm = addInfoComm;
exports.getInfoComm = getInfoComm;
exports.commNum = commNum;
exports.getKnowList = getKnowList;
exports.isCollection = isCollection;
exports.isaaddFollow = isaaddFollow;
exports.delFollow = delFollow;
exports.addFollow = addFollow;
exports.knowDetail = knowDetail;

